<?php

// Namespace

namespace Loop;


// Dependencies

require_once(PATH_LOOP . 'Class/CDocumentation.php');


/**
 * CDocumentationManager allows to manage the necessary files for the documentation 	
 */

class CDocumentationManager
{
	/**
	 * Get the files from the given directory
	 *
	 * @param 	string 	$extensionId 	The extension id
	 * @param 	string 	$directory 		The directory
	 *
	 * @return 	array 	An associative array which contains the list of files in the directory sorted by type
	 */

	public function getFiles($extensionId, $directory)
	{
		// Create object documentation 

		$documentation = new CDocumentation();


		// Get the allowed extensions

		$allowedExtensions =  pref('[Loop]/documentation/allowedExtensions');


		// Get the path to extension

		$pathToExtension = app()->getPathHelper()->convertExtensionIdToPath($extensionId);


		// Open the directory

		$trunkDirectory = opendir($pathToExtension . $directory);


		// Get the directory's files

		$filesInDirectory= scandir($pathToExtension . $directory);


		// Initialize variable

		$files = array();


		// For each file found

		foreach ($filesInDirectory as $fileInDirectory)
		{
			// Is it a directory ?

			if (is_dir($fileInDirectory))
			{
				continue;
			}


			// Get the extension

			$extension = '.' . pathinfo($pathToExtension . $directory . $fileInDirectory , PATHINFO_EXTENSION);


			// Is the extention allowed ?

			if (array_search($extension, $allowedExtensions) === false)
		 	{
		 		// The extension is not allowed

		 		continue;
		 	}			


			// Calcul the length of the extension

			$length = strlen($extension);


			// Get the name of the file without extension

			$fileName = substr($fileInDirectory, 0, -$length);
			

			// Does the filename already exist in the array ?

			if (array_search($fileName, $files) !== false)
			{
				// The directory is excluded

				continue;
			}
			

			// Special case Loop/Proxy.php

			if
			(
				$fileName == 'Proxy' &&
				$extensionId == 'Loop' &&
				$directory == 'Class'
			)
			{
				continue;
			}


			// Define the path to the file to documentate

			$pathToFile = $pathToExtension . $directory . '/' . $fileName  . '.php';


			// Require the file

			require_once($pathToFile);		


			// Construct the object

			$objectName = $fileName;


			// Is the class existing?

			if (class_exists($objectName) === false)
			{
				// No, add namespace

				$namespace = str_replace('-', '\\', $extensionId);

				$objectName = $namespace . '\\' . $fileName;
			}


			// Instanciate the object reflection class

			$objectReflection = new \ReflectionClass($objectName);


			// Get the description

			$docComment = $objectReflection->getDocComment();
			
			
			// Does the method referer to another ?
		
			$documentation->getMethodReference($docComment);		
		
		
			$description = $documentation->getFileDescription($docComment);


			// Is it a factory ?

			preg_match_all('/^C.*Factory$/', $fileName, $matchesFactory);

			if(count($matchesFactory) > 0)
			{
				if (count($matchesFactory[0]) > 0)
				{
					$files['Factories'][$fileName] = $description;

					continue;
				}
			}

			// Is it an helper ?

			preg_match_all('/^C.*Helper$/', $fileName, $matchesHelpers);

			if(count($matchesHelpers) > 0)
			{
				if (count($matchesHelpers[0]) > 0)
				{
					$files['Helpers'][$fileName] = $description;

					continue;
				}
			}


			// Is it a manager ?

			preg_match_all('/^C.*Manager$/', $fileName, $matchesManagers);

			if(count($matchesManagers) > 0)
			{
				if (count($matchesManagers[0]) > 0)
				{
					$files['Managers'][$fileName] = $description;

					continue;
				}
			}


			// Is it a proxy ?

			preg_match_all('/^C.*Proxy$/', $fileName, $matchesProxy);

			if(count($matchesProxy) > 0)
			{
				if (count($matchesProxy[0]) > 0)
				{
					$files['Proxys'][$fileName] = $description;

					continue;
				}
			}


			// Is it a driver ?

			preg_match_all('/^C.*Driver$/', $fileName, $matchesDriver);

			if(count($matchesDriver) > 0)
			{
				if (count($matchesDriver[0]) > 0)
				{
					$files['Drivers'][$fileName] = $description;

					continue;
				}
			}


			// Is it an interface ?

			preg_match_all('/^I/', $fileName, $matchesInterfaces);

			if(count($matchesInterfaces) > 0)
			{
				if (count($matchesInterfaces[0]) > 0)
				{
					$files['Interfaces'][$fileName] = $description;

					continue;
				}
			}			

			$files['Files'][$fileName] = $description;

		}

		ksort($files);


		return $files;
	}
}
?>